/**
 *
 */
package com.naiterui.ehp.bs.doctor.controller.inner;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;
import com.naiterui.ehp.bs.doctor.service.IConsultSessionService;
import com.naiterui.ehp.bs.doctor.service.IDrMessageConsultChargeService;

/**
 * 内部服务聊天会话
 * @author guoyongxiang
 * @since 2.9.0
 */
@Controller
@RequestMapping("ad/inner/consult")
@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.INNER_API + SwaggerTag.GRAPHIC_CONSULT})
public class AdInnerConsultSessionController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AdInnerConsultSessionController.class);

    @Autowired
    private IConsultSessionService consultSessionService;

    @Autowired
    private IDrMessageConsultChargeService drMessageConsultChargeService;

    /**
     * 修改会话推药标识
     * InnerConsultSessionController.updateConsultSessionRecomFlag()
     * @Author guoyongxiang
     * @Date 2017年8月4日
     * @since 2.9.0
     * @param consultSessionId
     * @return
     */
    @RequestMapping("updateSessionRecomFlag")
    @ResponseBody
    public ResponseVO<Void> updateSessionRecomFlag(Long consultSessionId) {
        this.consultSessionService.updateSessionRecomFlag(consultSessionId);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 查询是否设置付费咨询
     * InnerConsultSessionController.checkIfNeedCharge()
     * @Author Ldl
     * @Date 2018年1月25日
     * @since 1.0.0
     * @param doctorId
     * @param patientId
     * @return
     */
    @RequestMapping("checkNeedCharge")
    @ResponseBody
    public ResponseVO<Integer> checkIfNeedCharge(Long doctorId, Long patientId) {
        LOGGER.info("checkIfNeedCharge 请求参数,doctorId:{},patientId:{}", doctorId, patientId);
        Integer charge = drMessageConsultChargeService.getChargeById(doctorId, patientId);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, charge > 0 ? 1 : 0);
    }

    /**
     * 修改会话状态
     *
     * @param sessionId
     * @param sessionStatus
     * @return
     * @throws BusinessException
     */
    @PostMapping("updateSessionStatus")
    public ResponseVO<Void> updateSessionStatus(String sessionId, Integer sessionStatus) throws BusinessException {
        this.consultSessionService.updateSessionStatus(sessionId, sessionStatus);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 根据IM会话ID查询咨询会话ID
     *
     * @param sessionId
     * @return
     * @throws BusinessException
     */
    @GetMapping("consultSessionId")
    public ResponseVO<Long> consultSessionId(String sessionId) throws BusinessException {
        Long id = this.consultSessionService.findIdBySessionId(sessionId);
        return PublicService.returnResponseVO(id);
    }
}
